library(ggdist)
library(tidyverse)
library(maps)
library(readxl)
library(mapproj)
library(patchwork)

BD <- read_dta("data/birthdata.dta")
BD <-BD%>% 
  mutate(year= as.factor(year))

###FIGURE 1###

panel1<- ggplot(BD) + 
  geom_col(aes(x = year, y = NPO, fill = NPO)) +
  scale_fill_gradient2()+
  geom_line(aes(x = year, y = changePO), size = 1.5, color="purple", group = 1) +
  labs(subtitle="Number of Parental Orders (registered surrogate births)",
       y="",
       x="")+
  geom_label(aes(x = year, y =changePO, label = round(changePO, 1)),
             size = 3, fontface="bold", color="purple")+
  scale_y_continuous(sec.axis = sec_axis(~./1, name = "% change from 2011"))+
theme_ggdist()+
  theme(legend.position = "none",
        plot.subtitle = element_text(face="bold"),
        axis.text.x = element_text(face="bold"))


panel2<- ggplot(BD) + 
  geom_col(aes(x = year, y = NIVF, fill = NIVF)) +
  scale_fill_gradient2()+
  geom_line(aes(x = year, y = 1000*changeIVF), size = 1.5, color="purple", group = 1) +
  labs(subtitle="Number of IVF Treatments",
       y="",
       x="")+
  geom_label(aes(x = year, y =1000*changeIVF, label = round(changeIVF, 1)),
             size = 3, fontface="bold", color="purple", nudge_y=3000)+
  scale_y_continuous(sec.axis = sec_axis(~./1000, name = "% change from 2011"))+
  theme_ggdist()+
  theme(legend.position = "none",
        plot.subtitle = element_text(face="bold"),
        axis.text.x = element_text(face="bold"))


panel2+panel1
ggsave("figures/Figure1.eps", width=24, height=18, unit="cm", dpi=900)

###FIGURE 4###

fullEUplus= c('UK', 'France', 'Germany', 'Italy', 'Spain',
              'Poland', 'Romania', 'Netherlands', 'Belgium',
              'Czech Republic', 'Greece', 'Portugal', 'Sweden',
              'Hungary', 'Austria', 'Switzerland', 'Cyprus',
              'Bulgaria', 'Denmark', 'Finland', 'Slovakia',
              'Ireland', 'Croatia',  'Slovenia', 'Latvia', 
              'Estonia', 'Luxembourg', 'Malta', 'Norway', 'Ukraine', 'Belarus', 'Moldova')

eu.mapsplus <- map_data("world", region = fullEUplus)
region.lab.data3 <- eu.mapsplus %>%
  group_by(region) %>%
  summarise(long = mean(long), lat = mean(lat))
dflaws = read_excel("data/gays_surrogacyLAWS.xls")

merged = eu.mapsplus %>% 
  left_join(dflaws, by = 'region')

region.laws <- merged %>%
  group_by(region) %>%
  summarise(long = mean(long), lat = mean(lat), law=law)

col1 <- c("deeppink1", "#F58ABE", "grey69", "steelblue3")

ggplot(merged, aes(x = long, y = lat)) +
  geom_polygon(aes( group = group,  fill = law))+ 
  scale_fill_manual(values = col1)+ 
  geom_label(data = region.laws,aes(label = region),
             size = 2, hjust = 0.5, fontface="bold")+
  theme_void()+
  labs(title="Legality of surrogacy arrangements in European jurisdictions",
       caption="EU27, Belarus, Norway, Moldova, Switzerland, the UK, and Ukraine (2021)\n
       *In Portugal women must demonstrate medicial need (e.g. no uterus or inhospitable uterus)")+
  theme(legend.title = element_blank(),
        legend.position = "bottom",
        plot.title = element_text(color="black", hjust = 0, 
                                  size=18, face=2),
        plot.caption = element_text(lineheight = 0.35))
ggsave("figures/Figure2.eps", width=20, height=20, unit="cm", dpi=900)


